import MySql.Goods;
import MySql.goodsManager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class AddGoodsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();

        String id=request.getParameter("id");
        String kind=request.getParameter("kind");
        String title=request.getParameter("title");
        String brief=request.getParameter("brief");
        String img=request.getParameter("img");
        Double price=Double.parseDouble(request.getParameter("price"));
        out.println(request.getParameter("price"));
        Goods good=new Goods(id,kind,title,brief,price,img);
        Boolean flag=false;
        Boolean flag1=false;
        out.println(good.toString());
        try {
            flag= goodsManager.testId(id);
            out.println(flag);
            if(flag){
                flag1=goodsManager.updateGood(good);
                out.println(flag1+"4589");
            }else{
                flag1=goodsManager.registerGood(good);
                out.println(flag1+"htfgj");
            }
        } catch (SQLException e) {
            out.println("出错了");
            e.printStackTrace();
        }
        if(flag1){
            //成功界面
           response.sendRedirect("manager.jsp#goods");
        }else{
            //跳转失败界面
           response.sendRedirect("fail.jsp");
        }

    }
}
